package com.tbit.uqbike.object.pojo;

import com.tbit.uqbike.constant.enums.AccountDayStatEnum;
import lombok.AllArgsConstructor;

import java.math.BigDecimal;
import java.util.Map;

@AllArgsConstructor
public class OperationalData {
    Map<String, Object> operationalData;

    /**
     * 其他收入
     *
     * @return
     */
    public BigDecimal getOtherIncome() {
        return new BigDecimal(getOtherDataMap().get("otherIncomeMoney").toString());
    }

    /**
     * 其他支出
     *
     * @return
     */
    public BigDecimal getOtherExpenses() {
        return new BigDecimal(getOtherDataMap().get("otherExpensesMoney").toString());
    }

    /**
     * 其他
     *
     * @return
     */
    public BigDecimal getOther() {
        return getOtherIncome().subtract(getOtherExpenses());
    }

    /**
     * 商户统计(公式=微信商户+支付宝商户+汇付商户)
     *
     * @return
     */
    public BigDecimal getMerchant() {
        return getSum(AccountDayStatEnum.REVENUE_FROM_MERCHANT_PLATFORM)
                .subtract(getSum(AccountDayStatEnum.MERCHANT_PLATFORM_EXPENDITURE));
    }

    /**
     * 微信
     *
     * @return
     */
    public BigDecimal getWechat() {
        return getSum(AccountDayStatEnum.WECHAT_MERCHANT_INCOME)
                .subtract(getSum(AccountDayStatEnum.WECHAT_MERCHANT_SPENDING));
    }

    /**
     * 支付宝
     *
     * @return
     */
    public BigDecimal getAlipay() {
        return getSum(AccountDayStatEnum.ALIPAY_MERCHANT_INCOME)
                .subtract(getSum(AccountDayStatEnum.ALIPAY_MERCHANT_EXPENSES));
    }

    /**
     * 汇付
     *
     * @return
     */
    public BigDecimal getRemittance() {
        return getSum(AccountDayStatEnum.REMITTANCE_OF_MERCHANT_INCOME)
                .subtract(getSum(AccountDayStatEnum.REMITTING_PAYMENTS_TO_MERCHANTS));
    }

    /**
     * 骑行收入
     *
     * @return
     */
    public BigDecimal getCyclingIncome() {
        return getSum(AccountDayStatEnum.CYCLING_INCOME);
    }

    /**
     * 用户罚款收入
     *
     * @return
     */
    public BigDecimal getUserFineIncome() {
        return getSum(AccountDayStatEnum.USER_FINE_INCOME);
    }

    /**
     * 购卡收入
     *
     * @return
     */
    public BigDecimal getPurchaseIncome() {
        return getSum(AccountDayStatEnum.PURCHASE_INCOME);
    }

    /**
     * 退款
     *
     * @return
     */
    public BigDecimal getRefund() {
        return getSum(AccountDayStatEnum.CYCLING_EXPENSES)
                .add(getSum(AccountDayStatEnum.USER_PENALTY_EXPENDITURE))
                .add(getSum(AccountDayStatEnum.PURCHASE_COSTS));
    }

    /**
     * 运营营收(公式=骑行费用+用户罚款+用户购卡-退款)
     *
     * @return
     */
    public BigDecimal getOperation() {
        return getSum(AccountDayStatEnum.OPERATING_INCOME)
                .subtract(getSum(AccountDayStatEnum.OPERATING_EXPENSES));
    }

    /**
     * 预存款收入，不包含手工
     *
     * @return
     */
    public BigDecimal getAdvanceDepositIncome() {
        return getSum(AccountDayStatEnum.USER_RECHARGE_INCOME)
                .add(getSum(AccountDayStatEnum.INCOME_FROM_CYCLING_REFUND));
    }

    /**
     * 预存款支出，不包含手工
     *
     * @return
     */
    public BigDecimal getAdvanceDepositExpenses() {
        return getSum(AccountDayStatEnum.USER_RECHARGE_EXPENDITURE)
                .add(getSum(AccountDayStatEnum.CYCLING_CONSUMPTION_EXPENDITURE));
    }

    /**
     * 预存款，不包含手工
     *
     * @return
     */
    public BigDecimal getAdvanceDeposit() {
        return getAdvanceDepositIncome()
                .subtract(getAdvanceDepositExpenses());
    }

    public BigDecimal getUserDepositIncome() {
        return getSum(AccountDayStatEnum.USER_DEPOSIT_INCOME);
    }

    public BigDecimal getUserDepositExpenses() {
        return getSum(AccountDayStatEnum.USER_DEPOSIT_EXPENDITURE);
    }

    public BigDecimal getUserDeposit() {
        return getUserDepositIncome()
                .subtract(getUserDepositExpenses());
    }

    public BigDecimal getMerchantStat() {
        return getOperation()
                .add(getOther())
                .add(getUserDeposit())
                .add(getAdvanceDeposit());
    }

    private Map<String, Object> getOtherDataMap() {
        return (Map<String, Object>) operationalData.get("otherDataMap");
    }

    private BigDecimal getSum(AccountDayStatEnum accountDayStatEnum) {
        return new BigDecimal(operationalData.get(accountDayStatEnum.getSumFieldName()).toString());
    }


}
